package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.common.CommonCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("adminCategoryService")
@Slf4j
public class CategoryServiceImpl implements com.sky.service.admin.CategoryService {

    private final CategoryMapper categoryMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;
    private final CommonCategoryService commonCategoryService;

    @Autowired
    public CategoryServiceImpl(CategoryMapper categoryMapper, DishMapper dishMapper, SetmealMapper setmealMapper, CommonCategoryService commonCategoryService) {
        this.categoryMapper = categoryMapper;
        this.dishMapper = dishMapper;
        this.setmealMapper = setmealMapper;
        this.commonCategoryService = commonCategoryService;
    }

    /**
     * 新建新分类
     * @param categoryDTO 分类 DTO
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        log.info("执行 创建新分类 业务: {}",categoryDTO);
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);

        categoryMapper.insertCategory(category);
    }

    /**
     * 根据id删除分类
     * @param categoryId 套餐id
     */
    @Override
    @Transactional
    public void deleteCategoryById(Long categoryId) {
        log.info("执行 根据id删除分类 业务: {}",categoryId);
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(categoryId);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(categoryId);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteCategoryById(categoryId);
    }

    /**
     * 根据分类实体类DTO来修改分类信息
     * @param categoryDTO 分类实体DTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        log.info("执行 根据分类实体类DTO来修改分类信息 业务: {}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
    }

    /**
     * 根据分类id启用或禁用改分类
     * @param status     分类状态
     * @param categoryId 分类id
     */
    @Override
    public void startOrStopCategory(Integer status, Long categoryId) {
        log.info("执行 根据分类id启用或禁用改分类 业务: {}",categoryId);
        Category category = Category.builder()
                .id(categoryId)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.updateCategory(category);
    }

    /**
     * 分页查询分类信息
     * @param categoryPageQueryDTO 分页分类DTO类
     * @return 分页分类数据包
     */
    @Override
    public PageResult pageQueryCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("执行 分页查询分类信息 业务: {}",categoryPageQueryDTO);
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        try (Page<Category> page = categoryMapper.pageQueryCategory(categoryPageQueryDTO)) {
            log.info("通过分页查询分类");
            return new PageResult(page.getTotal(), page.getResult());
        }
    }

    /**
     * 根据分类信息获取分类实体集合
     *
     * @param categoryType 分类信息
     * @return 分类实体类集合
     */
    @Override
    public List<Category> listCategory(Integer categoryType) {
        return commonCategoryService.listCategory(categoryType);
    }

}
